/**
 *
 * @param account
 * @param password
 *
 */
import {url} from '../util/config'
import $ from 'jquery'
import store from "../store/store";
import {message} from "antd";
export default function login(account,password) {
    // 首先react路由 判断 localstorage 有无token,如果没有通过账号密码发送请求拿到token
    console.log(account,password)
    let status = null
    $.ajax({
        async:false,
        // url:url.api_url+"/eipapi/v1/Module/GetModuleFlag",
        // url:url.api_url+`Basic/Login`,
        url:url.api_url+`Basic/Login`,
        method:"POST",
        contentType:"application/json",
        data:JSON.stringify({
            account:account,
            password:password
        }),
        // xhrFields: {
        //     withCredentials: true
        // },
        success:function(result){
            console.log(result)
            if(result.token==null){
                // result.errorMessage
                let account = prompt("請重新輸入賬戶","")
                let password = prompt("請重新輸入密碼","")
                localStorage.setItem("account",account)
                localStorage.setItem("password",password)
                status = false
                return status
            }

            if(result.dtFirst.length>=0){
                localStorage.setItem("hasExit","true")
                localStorage.setItem("token",result.token)
                localStorage.setItem("userType",result.userType)
                let arr1 = result.dtFirst.map(item => {
                    return {
                        moduleId: item.MODULE_ID,
                        moduleName: item.MODULE_NAME,
                        moduleParentId: item.MODULE_PARENT_ID,
                        moduleType: item.MODULE_TYPE,
                        moduleUrl: item.MODULE_URL,
                        sortNum:item.SORT_NUM
                    }
                })
                sessionStorage.setItem("getModule",JSON.stringify(arr1))
            }

            if(result.dtSecond.length>=0){
                let arr2 = result.dtSecond.map(item => {
                    return {
                        moduleId: item.MODULE_ID,
                        moduleName: item.MODULE_NAME,
                        moduleParentId: item.MODULE_PARENT_ID,
                        moduleType: item.MODULE_TYPE,
                        moduleUrl: item.MODULE_URL
                    }
                })
                sessionStorage.setItem("getAdminModule",JSON.stringify(arr2))
            }

            if(result.dtThird.length>=0){
                let arr3 = result.dtThird.map(item=>{
                    return {
                        moduleId: item.MODULE_ID,
                        moduleName: item.MODULE_NAME,
                        moduleParentId: item.MODULE_PARENT_ID,
                        moduleType: item.MODULE_TYPE,
                        moduleUrl: item.MODULE_URL,
                        sortNum:item.SORT_NUM,
                        moduleManage: item.MODULE_MANAGE_FLAG,       //   true/false
                        layoutType:item.LAYOUT_TYPE,
                        layoutArea:item.LAYOUT_AREA
                    }
                })
                localStorage.setItem("userName",result.userInfo.USER_NAME)
                localStorage.setItem("userDepartment",result.userInfo.USER_DEPARTMENT)
                localStorage.setItem("userDeptNo",result.userInfo.USER_DEPTNO)
                localStorage.setItem("userWorkNum",result.userInfo.USER_WORK_NUM)
                localStorage.setItem("userID",result.userInfo.USER_ID)
                sessionStorage.setItem("getManageModule",JSON.stringify(arr3))
            }
            sessionStorage.setItem("tab","index")
            status = true

        },
        error: function (error) {
            message.error("请求异常");
            status = false
        }
    });
    return status
}

